package com.central.apps.handler.inspcriterion;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.apps.dao.inspection.inspcriterionDao;
import com.central.apps.dao.inspection.inspregionDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class InspregionHandler extends BaseHandler {
    @Autowired
    private inspcriterionDao inspcriterionDao;
    @Autowired
    private inspregionDao inspregionDao;
    @Autowired
    private HandlerService handlerService;
    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("inspregionApp".equals(app)) {
            if ("gq_insparealocation".equals(objName)) {
                /**
                 * 在“位置”表中新建后选择位置时，根据用户所选树节点，将此节点下所有节点所关联的设备自动带入到设备表中，
                 * 且根据此设备分类与点检标准表中分类一致的标准编号写入设备表中。
                 */
                String id = MapUtils.getString(data, "id");
                String location = MapUtils.getString(data, "location");
                if (isUpdate) {
                    String getLocation = inspregionDao.getInsparealocationForLocationById(id);
                    if (!location.equals(getLocation)) {
                        inspregionDao.delInsparealineById(id);
                    }
                }else {

                }
                if (StringUtils.isNotBlank(location)) {
                    List<Map<String, Object>> getAssetList = handlerService.findBySql("select assetnum,description,classify from gq_assethead where location = '" + location + "'");
                    if (CollectionUtils.isNotEmpty(getAssetList)) {
                        for (int i = 0; i < getAssetList.size(); i++) {
                            Map<String, Object> assetMap = getAssetList.get(i);
                            String assetnum = MapUtils.getString(assetMap, "assetnum");
                            String description = MapUtils.getString(assetMap, "description");
                            String classify = MapUtils.getString(assetMap, "classify");
                            //获取存在此资产分类的点检标准
                            Map<String, Object> inspcriterionByclassify = inspcriterionDao.getInspcriterionByclassify(classify);
                            String inspnum = MapUtils.getString(inspcriterionByclassify, "inspnum");
                            String anAbstract = MapUtils.getString(inspcriterionByclassify, "abstract");
                            //将资产和标准保存至区域子表资产
                            Map<String, Object> insparealineMap = new HashMap<>();
                            insparealineMap.put("insplocid", id);
                            insparealineMap.put("assetnum", assetnum);
                            insparealineMap.put("assetdesc", description);
                            if (StringUtils.isNotBlank(inspnum)) {
                                insparealineMap.put("inspnum", inspnum);
                                insparealineMap.put("inspdesc", anAbstract);
                            }
                            inspregionDao.insertInsparealine(insparealineMap);
                        }
                    }
                }
            }
        }
    }
}
